package com.ljf.sw;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;

@AutoConfiguration
@EnableConfigurationProperties(SwaggerProperties.class)
@ConditionalOnClass({Docket.class})
@EnableOpenApi//启用swagger注解支持
public class SwaggerAutoConfigure {

    SwaggerProperties properties;
    public SwaggerAutoConfigure(SwaggerProperties properties) {
        this.properties = properties;
    }
    @Bean
    public Docket createDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())
                .enable(properties.isEnable())//为true可以访问 false不能访问
                .select()
                .apis(RequestHandlerSelectors.basePackage(properties.basePackage))
                .paths(PathSelectors.ant(properties.getMatchPath()))
                .build();
    }
    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .title(properties.title)
                .description(properties.description)
                .contact(new Contact(properties.getName(), properties.getHost(), properties.getEmail()))
                .version(properties.version)
                .build();
    }
}
